package com.example.flashsales.controller;

import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.GoodsDto;
import com.example.flashsales.entity.vo.GoodsTypeVo;
import com.example.flashsales.entity.vo.GoodsVo;
import com.example.flashsales.role.role;
import com.example.flashsales.service.impl.FileServiceImpl;
import com.example.flashsales.service.impl.GoodsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@CrossOrigin
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsServiceImpl goodsService;

    @Autowired
    FileServiceImpl fileService;

    /**
     * 查看所有商品
     *
     * @param page
     * @param num
     * @return
     */
    @ResponseBody
    @GetMapping("/viewAllGoods")
    public Result viewAllGoods(Integer page, Integer num) {
        if (page == null || num == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0) {
            return Result.error("页码不能小于等于0");
        }
        final List<GoodsVo> goodsVos = goodsService.viewAllGoods(num, page);
        if (goodsVos.isEmpty()) {
            return Result.error("抱歉 暂无数据");
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("total",goodsService.viewAllGoodsNum());
        map.put("message",goodsVos);
        final Result result = Result.ok("查看成功");
        result.setData(map);
        return result;
    }

    /**
     * 按类型查看商品
     *
     * @param typeId 类型id
     * @param num
     * @param page
     * @return
     */
    @ResponseBody
    @GetMapping("/viewGoodsByType")
    public Result viewGoodsByType(Integer typeId, Integer num, Integer page) {
        if (typeId == null || num == null || page == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0) {
            return Result.error("页数不能小于等于0");
        }
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setTypeId(typeId);
        final List<GoodsVo> goodsVos = goodsService.viewGoodsByType(goodsDto, num, page);
        if (goodsVos.isEmpty()) {
            return Result.error("抱歉 暂无数据");
        }
        Result result = Result.ok("查看成功");
        Map<Object,Object> map = new HashMap<>();
        map.put("total",goodsService.viewNumByType(typeId));
        map.put("message",goodsVos);
        result.setData(map);
        return result;
    }

    /**
     * 按名称查看商品
     *
     * @param name 商品名
     * @param num
     * @param page
     * @return
     */
    @ResponseBody
    @GetMapping("/viewGoodsByName")
    public Result viewGoodsByName(String name, Integer num, Integer page) {
        if (num == null || page == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0) {
            return Result.error("页数不能小于等于0");
        }
        final List<GoodsVo> goodsVos = goodsService.viewGoodsByName(name, num, page);
        if (goodsVos.isEmpty()) {
            return Result.error("抱歉 暂无数据");
        }
        final Result result = Result.ok("查看成功");
        Map<Object,Object> map = new HashMap<>();
        map.put("total",goodsService.viewNumByName(name));
        map.put("message",goodsVos);
        result.setData(map);
        return result;
    }


    /**
     * 查看商品的具体信息
     *
     * @param id 商品id
     * @return
     */
    @ResponseBody
    @GetMapping("/viewGoodsMessage")
    public Result viewGoodsMessage(Long id) {
        if (id == null){
            return Result.error("数据不能为空");
        }
        final GoodsVo goodsVo = goodsService.viewGoodsMessage(id);
        if (goodsVo == null) {
            return Result.error("抱歉你查看的商品不存在");
        }
        final Result result = Result.ok("查看成功");
        result.setData(goodsVo);
        return result;
    }


    /**
     * 查看所有类型
     *
     * @return
     */
    @GetMapping("/viewAllType")
    @ResponseBody
    public Result viewAllType() {
        final List<GoodsTypeVo> goodsTypeVos = goodsService.viewAllType();
        final Result result = Result.ok("查看成功");
        result.setData(goodsTypeVos);
        return result;
    }


    /**
     * 增加商品类型(管理人员 老板)
     *
     * @param name
     * @return
     */
    @role(role = 37)
    @ResponseBody
    @GetMapping("/addType")
    public Result addType(HttpServletRequest httpServletRequest,String name) {
        if (name == "" || name == null) {
            return Result.error("类型名称不能为空");
        }
        final List<GoodsTypeVo> goodsTypeVos = goodsService.viewAllType();
        boolean flag = true;
        for (GoodsTypeVo goodsTypeVo : goodsTypeVos) {
            if (goodsTypeVo.getName().equals(name)){
                flag = false;
            }
        }
        if (!flag){
            return Result.error("类型已存在");
        }
        final boolean b = goodsService.addType(name);
        if (b) {
            return Result.ok("增加成功");
        }
        return Result.error("增加失败");
    }


    /**
     * 修改类型
     *
     * @param typeId
     * @param type
     * @return
     */
    @role(role = 36)
    @ResponseBody
    @GetMapping("/updateType")
    public Result updateType(HttpServletRequest httpServletRequest,Integer typeId, String type) {
        if (typeId == null){
            return Result.error("数据不能为空");
        }
        final List<GoodsTypeVo> goodsTypeVos = goodsService.viewAllType();
        boolean flag = true;
        for (GoodsTypeVo goodsTypeVo : goodsTypeVos) {
            if (goodsTypeVo.getName().equals(type)){
                flag = false;
            }
        }
        if (!flag){
            return Result.error("类型已存在");
        }
        final boolean b = goodsService.updateType(typeId, type);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


//    /**
//     * 删除类型
//     *
//     * @param typeId
//     * @return
//     */
//    @role(role = 1)
//    @ResponseBody
//    @GetMapping("/deleteType")
//    public Result deleteType(HttpServletRequest httpServletRequest,Integer typeId) {
//        if (typeId == null){
//            return Result.error("数据不能为空");
//        }
////        GoodsServiceImpl goodsService = new GoodsServiceImpl();
//        final boolean b = goodsService.deleteType(typeId);
//        if (b) {
//            return Result.ok("删除成功");
//        }
//        return Result.error("删除失败");
//    }


    /**
     * 删除商品
     *
     * @param goodsId
     * @return
     */
    @role(role = 38)
    @ResponseBody
    @GetMapping("/deleteGoods")
    public Result deleteGoods(HttpServletRequest httpServletRequest,Long goodsId) {
        if (goodsId == null){
            return Result.error("数据不能为空");
        }
        final String s = goodsService.deleteGoods(goodsId);
        if (s.equals("删除成功")) {
            return Result.ok(s);
        }
        return Result.error(s);
    }


    /**
     * 修改商品描述
     *
     * @return
     */
    @role(role = 39)
    @ResponseBody
    @GetMapping("/updateDetails")
    public Result updateDetails(HttpServletRequest httpServletRequest,String details, Long goodsId) {
        if (goodsId == null) {
            return Result.error();
        }
        final boolean b = goodsService.updateDetails(details, goodsId);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * TODO 增加商品
     *
     * @param file    文件
     * @param name    商品名称
     * @param count   数量
     * @param price   价格
     * @param details 细节
     * @param typeId  类型id
     * @return
     */
    @role(role = 40)
    @PostMapping("/addGoods")
    @ResponseBody
    @ApiOperation(value = "文件上传")
    public Result addGoods(HttpServletRequest httpServletRequest,@RequestBody MultipartFile file, String name, Double count, Double price, String details, Integer typeId) {
        if (file == null || name == null || count == null || price == null || details == null || typeId == null || count <= 0.0 || price <= 0.0) {
            return Result.error("数据格式错误");
        }
        final List<GoodsTypeVo> goodsTypeVos = goodsService.viewAllType();
        boolean flag = true;
        for (GoodsTypeVo goodsTypeVo : goodsTypeVos) {
            if (goodsTypeVo.getId() == typeId){
                flag = false;
            }
        }
        if (flag){
            return Result.error("类型不已存在");
        }
        final List<String> list = goodsService.viewGoodsName();
        if (list.contains(name)){
            return Result.error("商品名称重复");
        }
        GoodsDto goodsDto = new GoodsDto(name, price, typeId, count,  details);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -30);
        date = calendar.getTime();
        goodsDto.setProductNumber(date.getTime());
        final String upload = fileService.upload(file);
        if (upload == null){
            return Result.error("上传失败");
        }
        goodsDto.setUrl(upload);
        if (goodsService.addGoods(goodsDto)) {
                return Result.ok("上传成功");
            }
        return Result.error("上传失败");
    }


    /**
     * 修改商品类型
     *
     * @param typeId
     * @param goodsId
     * @return
     */
    @role(role = 41)
    @ResponseBody
    @GetMapping("/updateGoodsType")
    public Result updateGoodsType(HttpServletRequest httpServletRequest,Integer typeId, Long goodsId) {
        if (typeId == null || goodsId == null) {
            return Result.error("给我传空我打死你");
        }
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setTypeId(typeId);
        goodsDto.setId(goodsId);
        if (goodsService.updateGoodsMessage(goodsDto)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改商品价格
     *
     * @param price
     * @param goodsId
     * @return
     */
    @role(role = 42)
    @ResponseBody
    @GetMapping("/updatePrice")
    public Result updatePrice(HttpServletRequest httpServletRequest,Double price, Long goodsId) {
        if (price == null || goodsId == null || price <= 0.0) {
            return Result.error("给我传空我打死你");
        }
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setPrice(price);
        goodsDto.setId(goodsId);
        if (goodsService.updateGoodsMessage(goodsDto)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改商品数量
     *
     * @param count
     * @param goodsId
     * @return
     */
    @role(role = 43)
    @ResponseBody
    @GetMapping("/updateCount")
    public Result updateCount(HttpServletRequest httpServletRequest,Double count, Long goodsId) {
        if (count == null || goodsId == null || count <= 0.0) {
            return Result.error("给我传空我打死你");
        }
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setCount(count);
        goodsDto.setId(goodsId);
        if (goodsService.updateGoodsMessage(goodsDto)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改商品名称
     *
     * @param name
     * @param goodsId
     * @return
     */
    @role(role = 44)
    @ResponseBody
    @GetMapping("/updateName")
    public Result updateName(HttpServletRequest httpServletRequest,String name, Long goodsId) {
        if (name == null || goodsId == null) {
            return Result.error("给我传空我打死你");
        }
        final List<String> list = goodsService.viewGoodsName();
        if (list.contains(name)){
            return Result.error("商品名称存在");
        }
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setName(name);
        goodsDto.setId(goodsId);
        if (goodsService.updateGoodsMessage(goodsDto)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改商品图片
     *
     * @param file
     * @param goodsId
     * @return
     */
    @role(role = 45)
    @ResponseBody
    @PostMapping("/updateUrl")
    public Result updateUrl(HttpServletRequest httpServletRequest,@RequestBody MultipartFile file, Long goodsId) {
        if (file == null || goodsId == null) {
            return Result.error("给我传空我打死你");
        }
        GoodsDto goodsDto = new GoodsDto();

        goodsDto.setId(goodsId);
//        String filePath = "D:\\Java-Spring\\FlashSales\\src\\main\\resources\\static\\img\\";
        final String upload = fileService.upload(file);
        if (upload == null){
            return Result.error("上传失败");
        }
        goodsDto.setUrl(upload);
        if (goodsService.updateGoodsMessage(goodsDto)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


}
